﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Net.Http.Formatting;
using MxTol.API.Helpers;
using System.Data;
using MxTol.Managers;

namespace MxTol.API.Controllers
{
    /// <summary>
    /// 财经法规题库API
    /// </summary>
    public class FrController : ApiController
    {
        // 返回json数据格式
        private static readonly JsonMediaTypeFormatter JSON_FORMATTER = new JsonMediaTypeFormatter();

        /// <summary>
        /// 获取财经法规20道单选题
        /// </summary>
        [HttpGet]
        public HttpResponseMessage GetSingle(string key, int isNew = 0)
        {
            DataTable table = null;
            try
            {
                if (SafeKeyHelper.VerifySafeKey(key))
                {
                    bool isNewSingle = isNew == 0 ? false : true;
                    table = FrSingleManager.GetSingle(isNewSingle);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogAsync("客户端调用获取财经法规单选题题库接口失败", ex);
            }

            Dictionary<string, DataTable> json = new Dictionary<string, DataTable>()
            {
                {"single",table}
            };

            return Request.CreateResponse(HttpStatusCode.OK, json, JSON_FORMATTER);
        }

        /// <summary>
        /// 获取财经法规20道多选题
        /// </summary>
        [HttpGet]
        public HttpResponseMessage GetMultiple(string key, int isNew = 0)
        {
            DataTable table = null;
            try
            {
                if (SafeKeyHelper.VerifySafeKey(key))
                {
                    bool isNewMultiple = isNew == 0 ? false : true;
                    table = FrMultipleManager.GetMultiple(isNewMultiple);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogAsync("客户端调用获取财经法规多选题题库接口失败", ex);
            }

            Dictionary<string, DataTable> json = new Dictionary<string, DataTable>()
            {
                {"multiple",table}
            };

            return Request.CreateResponse(HttpStatusCode.OK, json, JSON_FORMATTER);
        }

        /// <summary>
        /// 获取财经法规20道判断题
        /// </summary>
        [HttpGet]
        public HttpResponseMessage GetJudge(string key, int isNew = 0)
        {
            DataTable table = null;
            try
            {
                if (SafeKeyHelper.VerifySafeKey(key))
                {
                    bool isNewJudge = isNew == 0 ? false : true;
                    table = FrJudgeManager.GetJudge(isNewJudge);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogAsync("客户端调用获取财经法规判断题题库接口失败", ex);
            }

            Dictionary<string, DataTable> json = new Dictionary<string, DataTable>()
            {
                {"judge",table}
            };

            return Request.CreateResponse(HttpStatusCode.OK, json, JSON_FORMATTER);
        }

        /// <summary>
        /// 获取财经法规2道案例分析题
        /// </summary>
        [HttpGet]
        public HttpResponseMessage GetAnalysis(string key, int isNew = 0)
        {
            List<Dictionary<string, object>> list = null;
            try
            {
                if (SafeKeyHelper.VerifySafeKey(key))
                {
                    bool isNewAnalysis = isNew == 0 ? false : true;
                    list = FrAnalysisDescripeManager.GetDescripe(isNewAnalysis);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogAsync("客户端调用获取财经法规案例分析题库接口失败", ex);
            }

            Dictionary<string, List<Dictionary<string, object>>> json = new Dictionary<string, List<Dictionary<string, object>>>()
            {
                {"analysis",list}
            };

            return Request.CreateResponse(HttpStatusCode.OK, json, JSON_FORMATTER);
        }

        /// <summary>
        /// 获取财经法规所有题型
        /// </summary>
        [HttpGet]
        public HttpResponseMessage GetFr(string key, int isNew = 0)
        {
            DataTable singleTable = null;
            DataTable multipleTable = null;
            DataTable judgeTable = null;
            List<Dictionary<string, object>> list = null;
            try
            {
                if (SafeKeyHelper.VerifySafeKey(key))
                {
                    bool isNewFr = isNew == 0 ? false : true;

                    singleTable = FrSingleManager.GetSingle(isNewFr);
                    multipleTable = FrMultipleManager.GetMultiple(isNewFr);
                    judgeTable = FrJudgeManager.GetJudge(isNewFr);
                    list = FrAnalysisDescripeManager.GetDescripe(isNewFr);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogAsync("客户端调用获取财经法规所有题型接口失败", ex);
            }

            Dictionary<string,object> json = new Dictionary<string, object>()
            {
                {"single",singleTable},
                {"multiple",multipleTable},
                {"judge",judgeTable},
                {"analysis",list}
            };

            return Request.CreateResponse(HttpStatusCode.OK, json, JSON_FORMATTER);
        }
    }
}
